package com.lc.recovery.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.recovery.R;
import com.lc.recovery.app.AppActivity;
import com.lc.recovery.http.request.CodeLoginApi;
import com.lc.recovery.http.request.GetCodeApi;
import com.lc.recovery.http.response.LoginBean;
import com.lc.recovery.http.response.PublicMsg;
import com.lc.recovery.manager.ActivityManager;
import com.lc.recovery.other.IntentKey;
import com.lc.recovery.util.SPUtil;
import com.lc.recovery.util.constant.EvenConstant;
import com.lc.recovery.util.eventbus.Event;
import com.lc.recovery.util.eventbus.EventBusUtils;
import com.lc.widget.view.ClearEditText;
import com.lc.widget.view.CountdownView;
import com.lc.widget.view.RegexEditText;

import androidx.appcompat.widget.AppCompatImageView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * author : Android 轮子哥
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2018/10/18
 * desc   : 登录界面
 */
public final class LoginActivity extends AppActivity {


    @BindView(R.id.iv_login_logo)
    AppCompatImageView ivLoginLogo;
    @BindView(R.id.et_login_phone)
    ClearEditText etLoginPhone;
    @BindView(R.id.et_login_code)
    RegexEditText etLoginCode;
    @BindView(R.id.cv_phone_reset_countdown)
    CountdownView cvPhoneResetCountdown;

    @Override
    protected int getLayoutId() {
        return R.layout.login_activity;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        String type= getIntent().getStringExtra("type");
    }

    @OnClick({R.id.cv_phone_reset_countdown, R.id.tv_register, R.id.tv_login_forget, R.id.ll_login_body, R.id.btn_login_commit,R.id.tv_password_login})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cv_phone_reset_countdown:
                if (etLoginPhone.getText().toString().length() != 11) {
                    etLoginPhone.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.shake_anim));
                    toast(R.string.common_phone_input_error);

                    return;
                }



                // 隐藏软键盘
                hideKeyboard(getCurrentFocus());

                // 获取验证码
                EasyHttp.post(this)
                        .api(new GetCodeApi()
                                .setMobile(etLoginPhone.getText().toString()).setType("2"))
                        .request(new HttpCallback<PublicMsg>(this) {

                            @Override
                            public void onSucceed(PublicMsg data) {
                                if (data.getCode()==200) {
                                    toast(R.string.common_code_send_hint);
                                    cvPhoneResetCountdown.start();
                                }else {
                                    toast(data.getMsg());
                                }
                            }
                            @Override
                            public void onFail(Exception e) {
//                                super.onFail(e);

                            }
                        });


                break;
            case R.id.tv_register:
                startActivity(RegisterActivity.class);
                finish();
                break;
            case R.id.tv_login_forget:
                startActivity(PasswordForgetActivity.class);
                break;
            case R.id.ll_login_body:
                break;
            case R.id.btn_login_commit:
                login();

                break;
            case R.id.tv_password_login:
                startActivity(LoginPassActivity.class);
                finish();
                break;
        }
    }

    private void login() {
        // 获取验证码
        EasyHttp.post(this)
                .api(new CodeLoginApi()
                        .setMobile(etLoginPhone.getText().toString()).setCode(etLoginCode.getText().toString()))
                .request(new HttpCallback<LoginBean>(this) {

                    @Override
                    public void onSucceed(LoginBean data) {
                        if (data.getCode()==200){
                            SPUtil.put(LoginActivity.this, IntentKey.USERID,data.getUser().getId());
                            SPUtil.put(LoginActivity.this, IntentKey.TYPE,data.getUser().getType()+"");
//                                     btnLoginCommit.showSucceed();
                            // 进行内存优化，销毁除登录页之外的所有界面
                            ActivityManager.getInstance().finishAllActivities(HomeActivity.class);
                            EventBusUtils.sendEvent(new Event(EvenConstant.TYPE, data.getUser().getType()));
                            startActivity(HomeActivity.class);

                        }else {
                            toast(data.getMsg());

                        }
                    }
                    @Override
                    public void onFail(Exception e) {
//                                super.onFail(e);

                    }
                });

    }
}